/**
 权限组
 **/
(function(vc) {

    vc.extends({
        data: {
            assetImportInfo: {
                communityId: vc.getCurrentCommunity().communityId,
                excelTemplate: '',
                remark: ""
            }
        },

        _initMethod: function() {

        },
        _initEvent: function() {

        },
        methods: {
            assetImportValidate: function() {
                return vc.validate.validate({
                    assetImportInfo: vc.component.assetImportInfo
                }, {

                    'assetImportInfo.excelTemplate': [{
                        limit: "required",
                        param: "",
                        errInfo: "文件不能为空"
                    }],
                    'assetImportInfo.communityId': [{
                        limit: "required",
                        param: "",
                        errInfo: "还未入驻小区，请先入驻小区"
                    }]
                });
            },
            _openDownloadHcExcelTemplate: function() {
                //下载 模板
                vc.jumpToPage('/import/hc.xlsx')
            },
            getExcelTemplate: function(e) {
                //console.log("getExcelTemplate 开始调用")
                vc.component.assetImportInfo.excelTemplate = e.target.files[0];
            },
            _importData: function() {

                if (!vc.component.assetImportValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                // 导入数据
                if (!vc.component.checkFileType(vc.component.assetImportInfo.excelTemplate.name.split('.')[1])) {
                    vc.toast('不是有效的Excel格式');
                    return;
                }
                if (!vc.component.checkFileSize(vc.component.assetImportInfo.excelTemplate.size)) {
                    vc.toast('Excel文件大小不能超过2M');
                    return;
                }
                var param = new FormData();
                param.append("uploadFile", vc.component.assetImportInfo.excelTemplate);
                param.append('communityId', vc.component.assetImportInfo.communityId);


                vc.http.upload(
                    'assetImport',
                    'importData',
                    param, {
                        emulateJSON: true,
                        //添加请求头
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function(json, res) {
                        if (res.status == 200) {
                            //关闭model
                            vc.toast("处理成功");
                            vc.jumpToPage('/#/pages/property/listOwner')
                            return;
                        }
                        vc.toast(json, 10000);
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        if (errInfo.indexOf("<html>") > 0) {
                            vc.toast("数据提交导入，请到导入日志中查看导入是否成功");
                        } else {
                            vc.toast(errInfo);
                        }
                    });
            },
            _exitCommunityData: function() {
                vc.jumpToPage('/callComponent/assetImport/exitCommunityData?communityId=' + vc.getCurrentCommunity().communityId);
            },
            _openAssetImportLog: function() {
                vc.jumpToPage('/#/pages/property/assetImportLog')
            },
            checkFileType: function(fileType) {
                const acceptTypes = ['xls', 'xlsx'];
                for (var i = 0; i < acceptTypes.length; i++) {
                    if (fileType === acceptTypes[i]) {
                        return true;
                    }
                }
                return false;
            },
            checkFileSize: function(fileSize) {
                //2M
                const MAX_SIZE = 2 * 1024 * 1024;
                if (fileSize > MAX_SIZE) {
                    return false;
                }
                return true;
            }

        }
    });

})(window.vc);